﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class ThuChiReduceBOCollection : Csla.BusinessListBase<ThuChiReduceBOCollection, ThuChiReduceBO>
    {
        public Hashtable hstBoPhan = null;
        public Hashtable hstNguoi = null;
        public Hashtable hstQuyenSo = null;
        public Hashtable hstSo = null;
        public Hashtable hstNo = null;
        public Hashtable hstCo = null;

        #region BindingList Overrides
        protected override object AddNewCore()
        {
            ThuChiReduceBO _ThuChiReduceBO = ThuChiReduceBO.NewChildThuChiReduceBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_ThuChiReduceBO));
            Add(_ThuChiReduceBO);
            return _ThuChiReduceBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private ThuChiReduceBOCollection()
        {
        }

        public static ThuChiReduceBOCollection NewThuChiReduceBOCollection()
        {
            return new ThuChiReduceBOCollection();
        }

        public static ThuChiReduceBO NewThuChiReduceBO()
        {
            return ThuChiReduceBO.NewChildThuChiReduceBO();
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection()
        {
            SPParam spParam = new SPParam("ThuChiID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("ThuChiID", "ASC", "");
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(string where)
        {
            SPParam spParam = new SPParam("ThuChiID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("ThuChiID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThuChiID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThuChiID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThuChiID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }

        public static ThuChiReduceBOCollection GetThuChiReduceBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "ThuChiID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<ThuChiReduceBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiReduceBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                hstBoPhan = new Hashtable();
                hstNguoi = new Hashtable();
                hstQuyenSo = new Hashtable();
                hstSo = new Hashtable();
                hstNo = new Hashtable();
                hstCo = new Hashtable();

                while (dr.Read())
                {
                    ThuChiReduceBO objThuChiReduceBO = ThuChiReduceBO.GetChildThuChiReduceBO(dr);
                    if (string.IsNullOrEmpty(objThuChiReduceBO.BoPhan) == false)
                        if (hstBoPhan.ContainsKey(objThuChiReduceBO.BoPhan) == false)
                            hstBoPhan.Add(objThuChiReduceBO.BoPhan, true);

                    if (string.IsNullOrEmpty(objThuChiReduceBO.HoTen) == false)
                        if (hstNguoi.ContainsKey(objThuChiReduceBO.HoTen) == false)
                            hstNguoi.Add(objThuChiReduceBO.HoTen, true);

                    if (string.IsNullOrEmpty(objThuChiReduceBO.QuyenSo) == false)
                        if (hstQuyenSo.ContainsKey(objThuChiReduceBO.QuyenSo) == false)
                            hstQuyenSo.Add(objThuChiReduceBO.QuyenSo, true);

                    if (string.IsNullOrEmpty(objThuChiReduceBO.So) == false)
                        if (hstSo.ContainsKey(objThuChiReduceBO.So) == false)
                            hstSo.Add(objThuChiReduceBO.So, true);

                    if (string.IsNullOrEmpty(objThuChiReduceBO.No) == false)
                        if (hstNo.ContainsKey(objThuChiReduceBO.No) == false)
                            hstNo.Add(objThuChiReduceBO.No, true);

                    if (string.IsNullOrEmpty(objThuChiReduceBO.Co) == false)
                        if (hstCo.ContainsKey(objThuChiReduceBO.Co) == false)
                            hstCo.Add(objThuChiReduceBO.Co, true);


                    this.Add(objThuChiReduceBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public ThuChiReduceBO GetThuChiReduceBO(int _ThuChiID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].ThuChiID == _ThuChiID)
                    return this[i];

            return null;
        }

        public void Remove(int ThuChiID)
        {
            int length = this.Count;
            for (int i = 0; i < length; i++)
            {
                if (this[i].ThuChiID == ThuChiID)
                {
                    try
                    {
                        this.RemoveAt(i);
                    }
                    catch { }
                    break;
                }
            }
        }
        #endregion Business Method
        //==========================================================================
    }
}
